package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import entity.PageResult;
import entity.QueryPageBean;
import entity.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ceshi
 * @Title:检查项管理
 * @Package
 * @Description:
 * @date 2021/7/11/001111:06
 */
@RestController
@RequestMapping("checkItem")
public class CheckItemController {

    //通过注册中心来获得服务
    @Reference
    private CheckItemService checkItemService;

    /**
     * 添加数据
     */
    @RequestMapping("/add.do")
    public Result add(@RequestBody CheckItem checkItem) {
        checkItemService.add(checkItem);
        return new Result(true,"添加成功！");
    }

    /**
     * 分页查询
     */
    @RequestMapping("/findByPage.do")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult result = checkItemService.findByPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return result;
    }
    /**
     * 删除数据
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")//权限
    @RequestMapping("/deleteById.do")
    public Result delete(Integer id) {
        try{
            checkItemService.checkItemDelete(id);
            return new Result(true,"删除成功！");
        }catch (Exception ex){
            return new Result(false,ex.getMessage());
        }

    }

    /**
     * 查询单条数据
     */
    @RequestMapping("/findById.do")
    public CheckItem findById(Integer id) {
        return checkItemService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/findAll.do")
    public List<CheckItem> findAll() {
        return checkItemService.findAll();
    }

    /**
     * 编辑
     * @return
     */
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody CheckItem checkItem) {
        try{
            checkItemService.edit(checkItem);
            return new Result(true,"编辑成功");
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,"编辑失败");
        }
    }

}
